# 打开未开始的比赛（正在售卖的比赛）

from loaddata.mgDB import JinCaiDatabase,Wan500Database
from loaddata.loadData import LoadDataJinCai
from analysis import util

# 总进球正在售卖的
def getSellingTtgDatas():
    db = JinCaiDatabase()
    ds = db.get_selling({})
    dx = ds[0].get("value").get("matchInfoList")
    # 数据格式化，分析赔率
    dlist = []
    for d in dx:
        re = {"businessDate":d.get("businessDate"),"weekday":d.get("weekday")}
        subMatchList = d.get("subMatchList")
        datas = []
        for m in subMatchList:
            if m.get("matchStatus") == 'Selling':
                awayTeamAbbName = m.get("awayTeamAbbName")
                homeTeamAbbName = m.get("homeTeamAbbName")
                ttg = m.get("fixedBonus").get("oddsHistory").get("ttgList")[-1]
                qiu = {"s0":float(ttg.get("s0")),"s1":float(ttg.get("s1")),"s2":float(ttg.get("s2")),"s3":float(ttg.get("s3")),"s4":float(ttg.get("s4")),"s5":float(ttg.get("s5")),"s6":float(ttg.get("s6")),"s7":float(ttg.get("s7"))}
                qius = [float(ttg.get("s0")), float(ttg.get("s1")), float(ttg.get("s2")),
                       float(ttg.get("s3")), float(ttg.get("s4")), float(ttg.get("s5")),
                       float(ttg.get("s6")), float(ttg.get("s7"))]
                # 转化带有赔数的数据格式{"s0":[10.5,6]}
                res = util.to_pei(qiu)
                peid = util.to_pei_list(qiu)
                infos = {"NO":m.get("matchNumStr"),"details":res,"peid":peid,"qius":qius,"matchDate":m.get("matchDate"),"matchTime":m.get("matchTime"),"matchId":m.get("matchId"),"awayTeamAbbName": awayTeamAbbName, "homeTeamAbbName": homeTeamAbbName}
                datas.append(infos)

        re["datas"] = datas
        dlist.append(re)
    return dlist

# 半全场正在售卖的
def getSellingHafuDatas():
    db = JinCaiDatabase()
    ds = db.get_selling({})
    dx = ds[0].get("value").get("matchInfoList")
    # 数据格式化，分析赔率
    dlist = []
    for d in dx:
        re = {"businessDate":d.get("businessDate"),"weekday":d.get("weekday")}
        subMatchList = d.get("subMatchList")
        datas = []
        for m in subMatchList:
            if m.get("matchStatus") == 'Selling':
                awayTeamAbbName = m.get("awayTeamAbbName")
                homeTeamAbbName = m.get("homeTeamAbbName")
                org = m.get("fixedBonus").get("oddsHistory").get("hafuList")[-1]
                qiu = {"hh":float(org.get("hh")),"hd":float(org.get("hd")),"ha":float(org.get("ha")),
                       "dh":float(org.get("dh")),"dd":float(org.get("dd")),"da":float(org.get("da")),
                       "ah":float(org.get("ah")),"ad":float(org.get("ad")),"aa":float(org.get("aa"))}
                qius = [float(org.get("hh")), float(org.get("hd")), float(org.get("ha")),
                       float(org.get("dh")), float(org.get("dd")), float(org.get("da")),
                       float(org.get("ah")), float(org.get("ad")), float(org.get("aa"))]
                # 转化带有赔数的数据格式{"s0":[10.5,6]}
                res = util.to_pei(qiu)
                peid = util.to_pei_list(qiu)
                infos = {"NO":m.get("matchNumStr"),"details":res,"peid":peid,"qius":qius,"matchDate":m.get("matchDate"),"matchTime":m.get("matchTime"),"matchId":m.get("matchId"),"awayTeamAbbName": awayTeamAbbName, "homeTeamAbbName": homeTeamAbbName}
                datas.append(infos)

        re["datas"] = datas
        dlist.append(re)
    return dlist

# 胜平负--正在售卖的
def getSellingHDADatas():
    # 获取前，先更新一下数据
    db = JinCaiDatabase()
    ds = db.get_selling({})
    dx = ds[0].get("value").get("matchInfoList")
    # 数据格式化，分析赔率
    dlist = []
    for d in dx:
        re = {"businessDate":d.get("businessDate"),"weekday":d.get("weekday")}
        subMatchList = d.get("subMatchList")
        datas = []
        for m in subMatchList:
            if m.get("matchStatus") == 'Selling':
                awayTeamAbbName = m.get("awayTeamAbbName")
                homeTeamAbbName = m.get("homeTeamAbbName")
                oddsHistory = m.get("fixedBonus").get("oddsHistory")
                singleList = oddsHistory.get("singleList")
                leagueAbbName = oddsHistory.get("leagueAbbName")
                # 将开放单关的信息放到列表里面
                slist = []
                for s in singleList:
                    if s.get("single") == 1:
                        slist.append(s.get("poolCode"))
                # 胜平负
                hadList = oddsHistory.get("hadList")
                h = 0
                d = 0
                a = 0
                if hadList:
                    had = oddsHistory.get("hadList")[-1]
                    h = float(had.get("h"))
                    d = float(had.get("d"))
                    a = float(had.get("a"))

                spf = {"h":h, "d":d, "a":a}
                spfv = [h, d, a]
                # 转化带有赔数的数据格式{"s0":[10.5,6]}
                res_spf = util.to_pei(spf)
                peid_spf = util.to_pei_list(spf)

                # 让球胜平负
                hhad = oddsHistory.get("hhadList")[-1]
                # 让球数
                goalLine = hhad.get("goalLine")
                hspf = {"h": float(hhad.get("h")), "d": float(hhad.get("d")), "a": float(hhad.get("a"))}
                hspfv = [float(hhad.get("h")), float(hhad.get("d")), float(hhad.get("a"))]
                # 转化带有赔数的数据格式{"s0":[10.5,6]}
                res_hspf = util.to_pei(hspf)
                peid_hspf = util.to_pei_list(hspf)
                infos = {"NO": m.get("matchNumStr"), "goalLine": goalLine,"singleList":slist,"leagueAbbName":leagueAbbName,
                         "details": {"had": res_spf, "hspf": res_hspf}, "peid_spf": peid_spf, "peid_hspf": peid_hspf,
                         "spfv": spfv, "hspfv": hspfv, "matchDate": m.get("matchDate"), "matchTime": m.get("matchTime"),
                         "matchId": m.get("matchId"),"awayTeamAbbName": awayTeamAbbName, "homeTeamAbbName": homeTeamAbbName}

                datas.append(infos)

        re["datas"] = datas
        dlist.append(re)
    return dlist



if __name__ == '__main__':
    pass
    # 最早的竞猜记录时间2014-10-22
    # start = '2024-02-01'
    # end = '2024-02-03'
    # rs = moniter(start,end)
    #
    # 定义一个包含多个字典的列表
    # data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 25}]
    #
    # 按年龄进行分组
    # grouped_dicts = []
    # for key, group in groupby(sorted(data), lambda x: x['age']):
    #     grouped_dicts.append({'key': key, 'values': list(group)})
    #
    # print(grouped_dicts)

    # dict= {'a':1,'b':4,'c':2}
    # # 排序
    # ds = sorted(dict.items(),key=lambda x:x[1],reverse=False)
    # print(ds)
    # res = {}
    # for i in range(len(ds)):
    #     d = ds[i]
    #     res[d[0]] = (d[1],i+1)
    # print(res)



